package com.qyhl.school.school.column;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.qyhl.school.R;
import com.qyhl.school.R2;
import com.qyhl.school.school.SchoolHomeActivity;
import com.qyhl.school.school.column.list.SchoolColumnListFragment;
import com.qyhl.webtv.basiclib.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SchoolColumnFragment extends BaseFragment {
    @BindView(R2.id.tabLayout)
    SlidingTabLayout tabLayout;
    @BindView(R2.id.view_pager)
    ViewPager viewPager;
    @BindView(R2.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R2.id.title)
    ImageView title;
    @BindView(R2.id.back_btn)
    ImageButton backBtn;


    public static SchoolColumnFragment newInstance() {
        SchoolColumnFragment fragment = new SchoolColumnFragment();
        return fragment;
    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_school_column_home, null);
    }

    @Override
    protected void create() {
        if (getActivity() instanceof SchoolColumnActivity) {
            titleLayout.setBackgroundResource(R.color.white);
            title.setImageResource(R.drawable.school_column_title_dark);
            backBtn.setImageResource(R.drawable.btn_back_default);
        } else {
            titleLayout.setBackgroundResource(R.drawable.school_title_bg);
            title.setImageResource(R.drawable.school_column_title);
            backBtn.setImageResource(R.drawable.btn_white_back_default);
        }
        List<String> nameList = new ArrayList<>();
        List<Fragment> fragments = new ArrayList<>();
        nameList.add("全部");
        nameList.add("小学");
        nameList.add("中学");
        nameList.add("高中");
        nameList.add("大专");
        nameList.add("本科");

        fragments.add(SchoolColumnListFragment.newInstance(0));
        fragments.add(SchoolColumnListFragment.newInstance(1));
        fragments.add(SchoolColumnListFragment.newInstance(2));
        fragments.add(SchoolColumnListFragment.newInstance(3));
        fragments.add(SchoolColumnListFragment.newInstance(4));
        fragments.add(SchoolColumnListFragment.newInstance(5));

        viewPager.setAdapter(new Adapter(getChildFragmentManager(), fragments, nameList));
        viewPager.setOffscreenPageLimit(fragments.size());
        tabLayout.setViewPager(viewPager);
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }


    @OnClick(R2.id.back_btn)
    public void onClick() {
        if (getActivity() instanceof SchoolHomeActivity) {
            ((SchoolHomeActivity) getActivity()).doFinish();
        } else if (getActivity() instanceof SchoolColumnActivity) {
            ((SchoolColumnActivity) getActivity()).doFinish();
        }
    }


    static class Adapter extends FragmentPagerAdapter {
        private List<Fragment> fragmentList;
        private List<String> names;

        Adapter(FragmentManager fm, List<Fragment> fragmentList, List<String> names) {
            super(fm);
            this.fragmentList = fragmentList;
            this.names = names;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return names.get(position);
        }
    }
}
